package com.wwj.cases;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.wwj.Constant.Constant;
import com.wwj.pojo.API;
import com.wwj.pojo.Case;
import com.wwj.pojo.JsonPathValidate;
import com.wwj.pojo.WriteBackData;
import com.wwj.utils.AuthenticationUtils;
import com.wwj.utils.DataUtils;
import com.wwj.utils.ExcelUtils;
import com.wwj.utils.HttpUtils;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.*;

import java.util.List;
import java.util.Set;

/**
 * 登录测试用例
 */
public class LoginCase extends BaseCase{
    private Logger log = Logger.getLogger(LoginCase.class);

    @BeforeSuite
    public void init(){        //套件执行初始化
        //配置参数化变量
        AuthenticationUtils.env.put(Constant.REGISTER_MOBILE_TEXT,Constant.REGISTER_MOBILE_VALUE);
        AuthenticationUtils.env.put(Constant.REGISTER_PASSWORD_TEXT,Constant.REGISTER_PASSWORD_VALUE);
        AuthenticationUtils.env.put(Constant.LOGIN_MOBILE_TEXT,Constant.LOGIN_MOBILE_VALUE);
        AuthenticationUtils.env.put(Constant.LOGIN_PASSWORD_TEXT,Constant.LOGIN_PASSWORD_VALUE);

    }
//
    @Test(dataProvider = "datas",description = "登录")
    public void testLogin (API api, Case c){
        //获取全局变量，进行替换，未知不知道有几个需要替换的变量
        String params = paramReplace(c.getParams());
        System.out.println("params:"+params);
        String sql = paramReplace(c.getSql());
        System.out.println("sql:"+sql);
        //参数需要设置回去
        c.setParams(params);
        c.setSql(sql);


        //1.调用接口
        log.info("======开始执行登录接口=========");
        String content = call(api, c);
        //2.添加回写内容
        log.info("========响应体回写============");
        addWriteBackData(c.getCaseId(),Constant.ACTUAL_RESPONSE_DATA_CELLNUM,content);
        //3.开始断言
        log.info("==========开始响应体断言=========");
        boolean assertResponseData = assertResponseData(c, content);

        //4.判断最终结果，响应断言和数据库断言一起进行判断
        log.info("===========开始响应体和数据库断言========");
        String passContent = (assertResponseData)?"Pass":"Fail";
        log.info("===========回写最终断言结果============");
        addWriteBackData(c.getCaseId(),Constant.ASSERT_CELLNUM,passContent);
        //报表断言
        Assert.assertEquals(passContent,"Pass");
    }

    @AfterSuite
    public void finish(){
        log.info("=======开始批量回写数据=======");
        //套件执行完，再执行批量回写
        ExcelUtils.write(Constant.EXCEL_PATH,1);
        log.info("=======执行结束=======");
    }

    @DataProvider(name = "datas")
    public Object[][] datas(){
        Object[][] datas= DataUtils.getAPIAndByAPIId("2");
        //{{API,Case},{},.....}
        return datas;

    }
}